﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RiskLib;


namespace StatementTest
{
    public partial class FrmStatementTest : Form
    {
        public FrmStatementTest()
        {
            InitializeComponent();
            foreach (FieldNameEnum fieldName in Statement.StatementFieldList)
            {
                dsStatement.CalculationRow row = dsStatement.Calculation.NewCalculationRow();
                row.FieldName = fieldName.ToString();
                
                dsStatement.Calculation.Rows.Add(row);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btnCaculate_Click(object sender, EventArgs e)
        {
            Statement statement = new Statement(DateTime.Today, StatementPeriodType.A, "", true, double.Parse(txtMarketPrice.Text));
            // first loop: set original values to statement instance
            foreach (dsStatement.CalculationRow row in dsStatement.Calculation.Rows)
            {
                FieldNameEnum fieldName=(FieldNameEnum)Enum.Parse(typeof(FieldNameEnum),row.FieldName);
                double v = 0;
                if (double.TryParse(row.OriginalValue,out v))
                    statement.SetFieldValue(fieldName, v);
            }

            // second loop: loop all value and calculate
            foreach (dsStatement.CalculationRow row in dsStatement.Calculation.Rows)
            {
                FieldNameEnum fieldName = (FieldNameEnum)Enum.Parse(typeof(FieldNameEnum), row.FieldName);
                try
                {
                    row.CalculatedValue = statement.GetFieldValue(fieldName).ToString();
                }
                catch (Exception ex)
                {
                    row.Message = ex.Message;
                }
            }
        }
    }
}
